﻿using CCWin.SkinControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win.CSkin.Test
{
    public partial class PopKeyboard : Form
    {
        private const int WS_EX_TOOLWINDOW = 0x00000080;
        private const int WS_EX_NOACTIVATE = 0x08000000;
        private const int WM_MOUSEACTIVATE = 0x0021;
        private const int MA_NOACTIVATE = 0x0003;

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= (WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
                cp.Parent = IntPtr.Zero;
                return cp;
            }
        }

        protected override void WndProc(ref Message m)
        {
            //If we're being activated because the mouse clicked on us...
            if (m.Msg == WM_MOUSEACTIVATE)
            {
                //Then refuse to be activated, but allow the click event to pass through (don't use MA_NOACTIVATEEAT)
                m.Result = (IntPtr)MA_NOACTIVATE;
            }
            else
            {
                base.WndProc(ref m);
            }
        }

        //模拟键盘API  和上面的API参数类型有区别   键值用Keys，大部分准确
        [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
        public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        public PopKeyboard()
        {
            InitializeComponent();
        }

        private void PopKeyboard_Load(object sender, EventArgs e)
        {
            btn0.Click += new EventHandler(key_Click);
            btn1.Click += new EventHandler(key_Click);
            btn2.Click += new EventHandler(key_Click);
            btn3.Click += new EventHandler(key_Click);
            btn4.Click += new EventHandler(key_Click);
            btn5.Click += new EventHandler(key_Click);
            btn6.Click += new EventHandler(key_Click);
            btn7.Click += new EventHandler(key_Click);
            btn8.Click += new EventHandler(key_Click);
            btn9.Click += new EventHandler(key_Click);
            btnBACKSPACE.Click += new EventHandler(key_Click);

            lbl0.Click += new EventHandler(lbl_Click);
            lbl1.Click += new EventHandler(lbl_Click);
            lbl2.Click += new EventHandler(lbl_Click);
            lbl3.Click += new EventHandler(lbl_Click);
            lbl4.Click += new EventHandler(lbl_Click);
            lbl5.Click += new EventHandler(lbl_Click);
            lbl6.Click += new EventHandler(lbl_Click);
            lbl7.Click += new EventHandler(lbl_Click);
            lbl8.Click += new EventHandler(lbl_Click);
            lbl9.Click += new EventHandler(lbl_Click);
            lblBACKSPACE.Click += new EventHandler(lbl_Click);
        }

        void key_Click(object sender, EventArgs e)
        {
            SkinButton btn = (SkinButton)sender;
            string strkey = btn.Name.Substring(3);

            SendKeys.Send("{" + strkey + "}");
        }

        void lbl_Click(object sender, EventArgs e)
        {
            Label lbl = (Label)sender;
            string strkey = lbl.Name.Substring(3);

            SendKeys.Send("{" + strkey + "}");
        }

        private void NumberClick(int number)
        {
            //int number = Convert.ToInt32(((SkinButton)sender).Text); //0 1 2 3 4 5 6 7 8 9
            switch (number)
            {
                case 0:
                    keybd_event(Keys.D0, 0, 0, 0);
                    break;
                case 1:
                    keybd_event(Keys.D1, 0, 0, 0);
                    break;
                case 2:
                    keybd_event(Keys.D2, 0, 0, 0);
                    break;
                case 3:
                    keybd_event(Keys.D3, 0, 0, 0);
                    break;
                case 4:
                    keybd_event(Keys.D4, 0, 0, 0);
                    break;
                case 5:
                    keybd_event(Keys.D5, 0, 0, 0);
                    break;
                case 6:
                    keybd_event(Keys.D6, 0, 0, 0);
                    break;
                case 7:
                    keybd_event(Keys.D7, 0, 0, 0);
                    break;
                case 8:
                    keybd_event(Keys.D8, 0, 0, 0);
                    break;
                case 9:
                    keybd_event(Keys.D9, 0, 0, 0);
                    break;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{1}");
        }

    }
}
